package com.buss.younger.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jweb.core.bean.PageMode;
import org.jweb.core.bean.ReplyDataMode;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.buss.common.service.SysServiceI;
import com.buss.younger.entity.HxOutstandingEmployee;
import com.buss.younger.trans.form.HxYoungerSearchForm;
import com.buss.younger.trans.vo.OutstandingEmployeeVO;

@Controller
@RequestMapping("/younger")
public class YoungController {

	@Autowired
	private SysServiceI sysService;
	
	@RequestMapping("/list.do")
	@ResponseBody
	public ReplyDataMode serviceList(HxYoungerSearchForm searchForm, HttpServletRequest request) {
		ReplyDataMode replyDataMode = new ReplyDataMode();
		CriteriaQuery criteriaQuery = searchForm.getCriteriaQuery();
		
		List<HxOutstandingEmployee> employees = this.sysService.list(criteriaQuery);
		List<OutstandingEmployeeVO> employeeVOs = new ArrayList<>();
		OutstandingEmployeeVO employeeVO = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		for(HxOutstandingEmployee hoe : employees) {
			employeeVO = new OutstandingEmployeeVO();
			employeeVO.copyEntity(hoe);
			
			if(hoe.getCreateDate() != null) {
				employeeVO.setCreateDate(format.format(hoe.getCreateDate()));
			}
			
			if(hoe.getUpdateDate() != null) {
				employeeVO.setUpdateDate(format.format(hoe.getUpdateDate()));
			}
			
			employeeVOs.add(employeeVO);
		}
		
		replyDataMode.setData(employeeVOs);
		replyDataMode.setStatusCode("200");
		replyDataMode.setSuccess(true);
		
		return replyDataMode;
	}
	
	@RequestMapping("/pagelist.do")
	@ResponseBody
	public ReplyDataMode servicePageList(HxYoungerSearchForm searchForm, HttpServletRequest request) {
		ReplyDataMode replyDataMode = new ReplyDataMode();
		CriteriaQuery criteriaQuery = searchForm.getCriteriaQuery();
		
		Map<String, Object> orderMap = new HashMap<>();
		orderMap.put("createDate", "desc");
		orderMap.put("updateDate", "desc");
		
		criteriaQuery.setOrder(orderMap);
		
		int pageNo = Integer.parseInt(searchForm.getPageNo());
		int pageSite = Integer.parseInt(searchForm.getPageSize());
		PageMode pageMode =  this.sysService.list4page(criteriaQuery, pageNo, pageSite);
		List<HxOutstandingEmployee> employees =pageMode.getResult();
		List<OutstandingEmployeeVO> employeeVOs = new ArrayList<>();
		OutstandingEmployeeVO employeeVO = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		for(HxOutstandingEmployee hoe : employees) {
			employeeVO = new OutstandingEmployeeVO();
			employeeVO.copyEntity(hoe);
			
			if(hoe.getCreateDate() != null) {
				employeeVO.setCreateDate(format.format(hoe.getCreateDate()));
			}
			
			if(hoe.getUpdateDate() != null) {
				employeeVO.setUpdateDate(format.format(hoe.getUpdateDate()));
			}
			
			employeeVOs.add(employeeVO);
		}
		
		pageMode.setResult(employeeVOs);
		
		replyDataMode.setData(pageMode);
		replyDataMode.setStatusCode("200");
		replyDataMode.setSuccess(true);
		
		return replyDataMode;
	}
	
}
